/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import javax.swing.JDialog;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JButton;

import who.tmap.ui.util.MiscUIHelper;

/**
 * @author stl
 *
 * add margins around the map
 */
public class JDialogAddMargins extends JDialog {
	
	private static final Integer DEFAULT_MARGIN =5;
	
    private boolean okClicked =false;
    private Integer leftMarginPC =null, rightMarginPC =null, topMarginPC =null, bottomMarginPC =null;
    
	private javax.swing.JPanel jContentPane = null;

	private JPanel jPanelFields = null;
	private JButton jButtonOK = null;
	private JLabel jLabelLeftMargin = null;
	private JTextField jTextFieldLeftMargin = null;
	private JLabel jLabelRightMargin = null;
	private JTextField jTextFieldRightMargin = null;
	private JLabel jLabelTopMargin = null;
	private JTextField jTextFieldTopMargin = null;
	private JLabel jLabelBottomMargin = null;
	private JTextField jTextFieldBottomMargin = null;
	
	private JPanel jPanelButtons = null;
	private JButton jButtonCancel = null;
	private JLabel jLabelSpace = null;
	
	/**
	 * This is the default constructor
	 */
	public JDialogAddMargins( Frame parent ) {
		super( parent );
		this.leftMarginPC =DEFAULT_MARGIN;
		this.rightMarginPC =DEFAULT_MARGIN;
		this.topMarginPC =DEFAULT_MARGIN;
		this.bottomMarginPC =DEFAULT_MARGIN;
		
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setTitle("Add Margins");
		this.setSize(300,200);
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo( null );
		
		getRootPane().setDefaultButton( getJButtonOK() );
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {

			GridLayout gridLayout1 = new GridLayout();
			jContentPane = new JPanel();
			jContentPane.setLayout(new BoxLayout(jContentPane, BoxLayout.Y_AXIS));
			jContentPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));		
			jContentPane.add(getJPanelFields(), null);
			jContentPane.add(getJPanelButtons(), null);
			
		}
		return jContentPane;
	}
	
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanelFields() {
		if (jPanelFields == null) {
			jPanelFields = new JPanel();
			GridLayout gridLayout3 = new GridLayout(4,2);
			
			jPanelFields.setLayout(gridLayout3);		    
		    
			jLabelLeftMargin = new JLabel();
			jLabelLeftMargin.setText("Left margin");
			jLabelTopMargin = new JLabel();			
			jLabelTopMargin.setText("Top margin");
			jLabelRightMargin = new JLabel();
			jLabelRightMargin.setText("Right margin");
			jLabelBottomMargin = new JLabel();
			jLabelBottomMargin.setText("Bottom margin");
			jPanelFields.add(jLabelLeftMargin, null);
			jPanelFields.add(getJTextFieldLeftMargin(), null);
			jPanelFields.add(jLabelRightMargin, null);
		
			jPanelFields.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
			jPanelFields.add(getJTextFieldRightMargin(), null);
			jPanelFields.add(jLabelTopMargin, null);
			jPanelFields.add(getJTextFieldTopMargin(), null);
			jPanelFields.add(jLabelBottomMargin, null);
			jPanelFields.add(getJTextFieldBottomMargin(), null);

		}
		return jPanelFields;
	}
	
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setText("OK");
			
			jButtonOK.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {    
				    jButtonOK_actionPerformed();
				}
			});
		}

		return jButtonOK;
	}
	
	private void jButtonOK_actionPerformed() {
		if ( validateAll() ) {
			okClicked =true;
			setVisible( false );
		}
	}
	
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJTextFieldLeftMargin() {
		if (jTextFieldLeftMargin == null) {
			jTextFieldLeftMargin = new JTextField();
			jTextFieldLeftMargin.setText( leftMarginPC.toString() + "%" );
			jTextFieldLeftMargin.setHorizontalAlignment(JTextField.RIGHT);

		}
		return jTextFieldLeftMargin;
	}
	
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJTextFieldRightMargin() {
		if (jTextFieldRightMargin == null) {
			jTextFieldRightMargin = new JTextField();
			jTextFieldRightMargin.setText( rightMarginPC.toString() + "%" );
			jTextFieldRightMargin.setHorizontalAlignment(JTextField.RIGHT);
		}
		return jTextFieldRightMargin;
	}
	
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJTextFieldTopMargin() {
		if (jTextFieldTopMargin == null) {
			jTextFieldTopMargin = new JTextField();
			jTextFieldTopMargin.setText( topMarginPC.toString() + "%" );
			jTextFieldTopMargin.setHorizontalAlignment(JTextField.RIGHT);
		}
		return jTextFieldTopMargin;
	}
	
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJTextFieldBottomMargin() {
		if (jTextFieldBottomMargin == null) {
			jTextFieldBottomMargin = new JTextField();
			jTextFieldBottomMargin.setText( bottomMarginPC.toString() + "%" );
			jTextFieldBottomMargin.setHorizontalAlignment(JTextField.RIGHT);
		}
		return jTextFieldBottomMargin;
	}
	
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			jLabelSpace = new JLabel();
			jPanelButtons = new JPanel();
			jPanelButtons.setLayout(new BoxLayout(jPanelButtons, BoxLayout.X_AXIS));
			jLabelSpace.setText("    ");
			jPanelButtons.add(getJButtonOK(), null);
			jPanelButtons.add(jLabelSpace, null);
			jPanelButtons.add(getJButtonCancel(), null);
		}
		return jPanelButtons;
	}
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setVisible(true);
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {    
				    setVisible( false );
				}
			});
		}
		return jButtonCancel;
	}
	
	public boolean okHasBeenClicked() {
	    return okClicked; 
	}	
	
	public Integer getBottomMarginPC() {
		return bottomMarginPC;
	}

	public Integer getLeftMarginPC() {
		return leftMarginPC;
	}

	public Integer getRightMarginPC() {
		return rightMarginPC;
	}

	public Integer getTopMarginPC() {
		return topMarginPC;
	}	
	
	public void setBottomMarginPC(Integer bottomMarginPC) {
		this.bottomMarginPC = bottomMarginPC;
	}

	public void setLeftMarginPC(Integer leftMarginPC) {
		this.leftMarginPC = leftMarginPC;
	}

	public void setRightMarginPC(Integer rightMarginPC) {
		this.rightMarginPC = rightMarginPC;
	}

	public void setTopMarginPC(Integer topMarginPC) {
		this.topMarginPC = topMarginPC;
	}

	private boolean validateAll() {
		leftMarginPC =validate( getJTextFieldLeftMargin().getText(), "Left margin" );
		if ( leftMarginPC != null ) {
			rightMarginPC =validate( getJTextFieldRightMargin().getText(), "Right margin" );
			
			if ( rightMarginPC != null ) {
				topMarginPC =validate( getJTextFieldTopMargin().getText(), "Top margin" );
				
				if ( topMarginPC != null ) {
					bottomMarginPC =validate( getJTextFieldBottomMargin().getText(), "Bottom margin" );
				}
				
			}
		}
		
		return leftMarginPC != null && rightMarginPC != null && topMarginPC != null && bottomMarginPC != null;
	}
	
	private Integer validate( String txt, String componentDesc ) {
		Integer val =null;

		try {
			String str =txt.replace("%","");
	        val =Integer.parseInt( str );
	    } catch( Exception e ) {
	    	new MiscUIHelper().displayErrorMessage( this, componentDesc + " does not contain a valid value." );
	    }
		
		return val;
	}
  }
